package com.english.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.english.entity.Banner;
import com.english.mapper.BannerMapper;
import com.english.service.BannerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 轮播图服务实现类
 * 
 * @author wlq
 * @since 2025-08-05
 */
@Service
@Transactional
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
    
    @Override
    public List<Banner> getActiveBanners() {
        return baseMapper.selectActiveBanners();
    }
    
    @Override
    public boolean addBanner(Banner banner) {
        // 设置上传时间
        banner.setUploadTime(new Date());
        // 如果没有设置状态，默认为上架
        if (banner.getStatus() == null) {
            banner.setStatus(1);
        }
        return save(banner);
    }
    
    @Override
    public boolean updateBanner(Banner banner) {
        // 更新时保持原有的上传时间
        Banner existingBanner = getById(banner.getId());
        if (existingBanner != null) {
            banner.setUploadTime(existingBanner.getUploadTime());
        }
        return updateById(banner);
    }
    
    @Override
    public boolean deleteBanner(Long id) {
        return removeById(id);
    }
    
    @Override
    public boolean toggleBannerStatus(Long id) {
        Banner banner = getById(id);
        if (banner != null) {
            // 切换状态：1变0，0变1
            banner.setStatus(banner.getStatus() == 1 ? 0 : 1);
            return updateById(banner);
        }
        return false;
    }
} 